package cn.schoolwow.quickdao.module.database.dml.common.flow;

import cn.schoolwow.quickdao.domain.database.dml.CheckStrategy;
import cn.schoolwow.quickdao.module.database.dml.common.domain.ManipulationOption;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.util.HashMap;
import java.util.Map;

public class CheckReasonInstanceMapFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        ManipulationOption manipulationOption = flowContext.checkInstanceData(ManipulationOption.class);
        Map<Object,String> instanceReasonMap = (Map<Object, String>) flowContext.getData("instanceReasonMap", new HashMap<String,Object>());

        if(!instanceReasonMap.isEmpty()){
            flowContext.remark("存在不符合字段约束的实例");
            if(CheckStrategy.CheckAndThrow.equals(manipulationOption.checkStrategy)){
                String reason = instanceReasonMap.values().iterator().next();
                throw new IllegalArgumentException("实例数组存在不符合字段约束的实例!个数:"+instanceReasonMap.size()+",部分违反字段约束原因:"+reason);
            }
        }
    }

    @Override
    public String name() {
        return "检查字段约束策略实例列表";
    }
}
